package jf.jufanzhibo.view;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.widget.RadioGroup;

import java.util.Timer;
import java.util.TimerTask;

import jf.jufanzhibo.R;

public class MainActivity extends AppCompatActivity {

    private RadioGroup mRg;
    private Fragment HomeFragment;
    private Fragment SigntFragment;
    private Fragment MineFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        init();
        initDao();

    }

    private void initDao() {
        mRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.bt_home:
                        select(0);
                        break;
                    case R.id.bt_sight:
                        select(1);
                        break;
                    case R.id.bt_mine:
                        select(2);
                        break;
                }
            }
        });
    }

    private void init() {
        //初始化第一个界面
        select(0);
        mRg = (RadioGroup) findViewById(R.id.main_sy);
    }

    private void select(int i) {
        FragmentManager manager = getSupportFragmentManager();//获得Fragment管理器
        FragmentTransaction ft = manager.beginTransaction();
        //开启一个事务
        hideFragment(ft);   //先隐藏 Fragment

        switch (i) {
            case 0:
                if (HomeFragment == null) {
                    HomeFragment = FragmentFactory.setFragment(i);
                    ft.add(R.id.main_fl, HomeFragment);
                } else {
                    ft.show(HomeFragment);
                }
                break;
            case 1:
                if (SigntFragment == null) {
                    SigntFragment = FragmentFactory.setFragment(i);
                    ft.add(R.id.main_fl, SigntFragment);
                } else {
                    ft.show(SigntFragment);
                }
                break;
            case 2:
                if (MineFragment == null) {
                    MineFragment = FragmentFactory.setFragment(i);
                    ft.add(R.id.main_fl, MineFragment);
                } else {
                    ft.show(MineFragment);
                }
                break;
        }
        ft.commit();   //提交事务
    }

    //隐藏所有Fragment
    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if (HomeFragment != null) {
            fragmentTransaction.hide(HomeFragment);
        }
        if (SigntFragment != null) {
            fragmentTransaction.hide(SigntFragment);
        }
        if (MineFragment != null) {
            fragmentTransaction.hide(MineFragment);
        }
    }
    //处理3秒内单击两次返回键退出程序
    private static Boolean isQuit = false;
    private Timer timer = new Timer();
    @Override
    public void onBackPressed() {
        if(isQuit){
            finish();
            System.exit(0);
            android.os.Process.killProcess(android.os.Process.myPid());
        }else {
            isQuit = true;
//            Toast.makeText(MainActivity.this, "再次单击返回键退出程序", Toast.LENGTH_SHORT).show();
            TimerTask task = new TimerTask() {
                @Override
                public void run() {
                    isQuit = false;
                }
            };
            timer.schedule(task,3000);
        }
    }
}
